package com.twb.message.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.twb.auth.entity.twbUser.dto.TwbUserDto;
import com.twb.core.base.entity.TwbPageInfo;
import com.twb.core.base.service.impl.BaseService;
import com.twb.core.exception.ServiceException;
import com.twb.core.util.BeanUtils;
import com.twb.core.util.ExcelUtils;
import com.twb.core.util.TwbPageUtil;
import com.twb.message.entity.messageReceivor.MessageReceivor;
import com.twb.message.entity.messageReceivor.dto.SearchMessageReceivorDto;
import com.twb.message.entity.messageReceivor.vo.SaveMessageReceivorVo;
import com.twb.message.entity.messageReceivor.vo.SearchMessageReceivorVo;
import com.twb.message.entity.messageReceivor.vo.UpdateMessageReceivorByIdVo;
import com.twb.message.mapper.MessageReceivorMapper;
import com.twb.message.service.IMessageReceivorService;
import com.twb.system.util.ExcelDataDicHandlerImpl;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName MessageReceivorService
 * @Description 系统消息接收人信息service
 * 
 * @author DHL
 * @date 2021-07-14 09:27:31
 */
@Service("messageReceivorService")
public class MessageReceivorService extends BaseService<MessageReceivorMapper,MessageReceivor> implements IMessageReceivorService {

	@Autowired
	MessageReceivorMapper messageReceivorMapper;

	@Override
	public TwbPageInfo<SearchMessageReceivorDto> searchMessageReceivor(SearchMessageReceivorVo vo) {
		TwbPageUtil.startPage(vo);
		List<SearchMessageReceivorDto> list = messageReceivorMapper.searchMessageReceivor(vo);
		return TwbPageUtil.of(list);
	}

	@Override
	public List<SearchMessageReceivorDto> searchMessageReceivorList(SearchMessageReceivorVo vo) {
		List<SearchMessageReceivorDto> list = messageReceivorMapper.searchMessageReceivor(vo);
		return list;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean saveMessageReceivor(SaveMessageReceivorVo vo) {

		MessageReceivor checkVo = new MessageReceivor();
		checkVo.setMessageId(vo.getMessageId());
		checkVo.setReceivorId(vo.getReceivorId());
		checkVo.setIsdel(0);
		long count = this.count(checkVo);
		if (count > 0) {
			return false;
		}

		MessageReceivor record = new MessageReceivor();
		BeanUtils.copyNotNullProperties(vo, record);
		record.setId(this.getUUID());
		this.setCreateInfo(record);
		this.setUpdateInfo(record);
		boolean result = this.save(record);
		return result;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean updateMessageReceivorById(UpdateMessageReceivorByIdVo vo) {
		MessageReceivor record = messageReceivorMapper.selectById(vo.getId());
		if (null == record) {
			throw new ServiceException("数据不存在");
		}

		if (!vo.getMessageId().equalsIgnoreCase(record.getMessageId()) && !vo.getReceivorId().equalsIgnoreCase(record.getReceivorId())) {
			MessageReceivor checkVo = new MessageReceivor();
			checkVo.setMessageId(vo.getMessageId());
			checkVo.setReceivorId(vo.getReceivorId());
			checkVo.setIsdel(0);
			long count = this.count(checkVo);
			if (count > 0) {
				return false;
			}
		}

		BeanUtils.copyNotNullProperties(vo, record);
		this.setUpdateInfo(record);
		boolean result = this.updateById(record);
		return result;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean deleteMessageReceivorById(String id) {
		boolean result = this.removeById(id);
		return result;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean deleteMessageReceivorByIds(String ids) {
		return this.removeByIds(Arrays.asList(ids.split(",")));
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int logicBatchDeleteMessageReceivorByIds(String ids) {
		List<String> list = new ArrayList<String>();
		Collections.addAll(list, ids.split(","));
		return messageReceivorMapper.logicBatchDeleteByIds(list);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void exportExcel(SearchMessageReceivorVo vo, HttpServletResponse response) throws Exception {
		TwbPageInfo<SearchMessageReceivorDto> pageInfo = this.searchMessageReceivor(vo);
		ExcelDataDicHandlerImpl excelDataDicHandlerImpl = new ExcelDataDicHandlerImpl();
		ExcelUtils.exportExcel(vo,"系统消息接收人信息", pageInfo.getList(), SearchMessageReceivorDto.class, response,excelDataDicHandlerImpl);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int importExcel(Map<String, String> map) {
		System.out.println(map.toString());
		return 0;
	}

	@Override
	public List<TwbUserDto> listMessageReceivorUsers(String messageId) {
		return messageReceivorMapper.listMessageReceivorUsers(messageId);
	}
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean deleteMessageReceivorByMessageId(String messageId) {
		QueryWrapper<MessageReceivor> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("message_id", messageId);
		int delete = messageReceivorMapper.delete(queryWrapper);
		return delete > 0;
	}

	@Override
	public int searchUserMessageNum(String projectId, String userId, String readStatus) {
		return messageReceivorMapper.searchUserMessageNum(projectId, userId, readStatus);
	}
	
}
